/*
 * Copyright 2010-2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.amazon.aws.demo.ec2;

import android.util.Log;

import com.amazon.aws.demo.AWSDemo;

import java.util.*;

import com.amazonaws.services.cloudwatch.AmazonCloudWatchClient;
import com.amazonaws.services.cloudwatch.model.*;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.ec2.model.DescribeInstancesResult;
import com.amazonaws.services.ec2.model.Instance;
import com.amazonaws.services.ec2.model.Reservation;
import com.amazonaws.services.simpledb.AmazonSimpleDB;
import com.amazonaws.services.simpledb.AmazonSimpleDBClient;
import com.amazonaws.services.simpledb.model.Attribute;
import com.amazonaws.services.simpledb.model.CreateDomainRequest;
import com.amazonaws.services.simpledb.model.DeleteAttributesRequest;
import com.amazonaws.services.simpledb.model.DeleteDomainRequest;
import com.amazonaws.services.simpledb.model.GetAttributesRequest;
import com.amazonaws.services.simpledb.model.GetAttributesResult;
import com.amazonaws.services.simpledb.model.Item;
import com.amazonaws.services.simpledb.model.ListDomainsRequest;
import com.amazonaws.services.simpledb.model.ListDomainsResult;
import com.amazonaws.services.simpledb.model.PutAttributesRequest;
import com.amazonaws.services.simpledb.model.ReplaceableAttribute;
import com.amazonaws.services.simpledb.model.SelectRequest;

public class Ec2 {

	private static AmazonEC2Client sdb = null;
	private static String nextToken = null;
	private static int prevNumDomains = 0;
	public static final String DOMAIN_NAME = "_domain_name";
		
	public static AmazonEC2 getInstance() {
        if ( sdb == null ) {
		    sdb = new AmazonEC2Client( AWSDemo.credentials );
            sdb.setEndpoint( "https://ec2.amazonaws.com:443" );
        }

        return sdb;
	}
	
	public static List<String> getDomainNames() {
		List<String> names =new ArrayList<String>();
        DescribeInstancesResult instanceDescription = getInstance().describeInstances();

        AmazonCloudWatchClient cloudWatchClient = new AmazonCloudWatchClient(AWSDemo.credentials);
        ListMetricsResult metricsResult = cloudWatchClient.listMetrics(new ListMetricsRequest().withNamespace("AWS/EC2"));

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);

        GetMetricStatisticsRequest metricStatisticsRequest = new GetMetricStatisticsRequest()
                .withNamespace("AWS/EC2").withMetricName("CPUUtilization").withUnit("Percent")
                .withStartTime(calendar.getTime()).withEndTime(Calendar.getInstance().getTime())
                .withStatistics("Average").withPeriod(180);
        GetMetricStatisticsResult metricStatisticsResult = cloudWatchClient.getMetricStatistics(metricStatisticsRequest);
        List<Datapoint> datapoints = metricStatisticsResult.getDatapoints();
        Log.d("StartDataPoint", "Started to list data points of size " + datapoints.size());
        for (Datapoint datapoint : datapoints) {
            Log.d("DataPointList", datapoint.toString());
        }

        Log.d("InstanceDetail", instanceDescription.toString());
		List<Reservation> reservations = instanceDescription.getReservations();
		for(Reservation res:reservations){
			List<Instance> instances = res.getInstances();
			for(Instance in: instances){
				names.add(in.getKeyName() == null ? "No-Name" : in.getKeyName());
			}
		}
		return names;
	}

	public static List<String> getMoreDomainNames() {
		return Collections.EMPTY_LIST;
	}
	
		
}
